package com.zzq.service.impl;

import com.zzq.domain.dto.SmilePointGoodsDTO;
import com.zzq.domain.entity.SmileClient;
import com.zzq.domain.entity.SmileCoupon;
import com.zzq.domain.search.PointGoodsSearchParamet;
import com.zzq.enumerate.GoodsStatusEnum;
import com.zzq.enumerate.GoodsTypeEnum;
import com.zzq.result.SmileResult;
import com.zzq.security.util.UserUtil;
import com.zzq.service.*;
import com.zzq.subservice.EarnPointsSubService;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClientGoodsServiceImpl implements ClientGoodsService {
    @Autowired
    SmilePointGoodsService pointGoodsService;

    @Autowired
    SmileClientService clientService;

    @Autowired
    EarnPointsSubService expensesPoints;

    @Autowired
    SmileCouponService couponService;

    @Override
    public SmileResult getPointGoodsDTOList(PointGoodsSearchParamet searchParamet) {
        searchParamet.setGoodsStatus(GoodsStatusEnum.SHELF.getName());
        List<SmilePointGoodsDTO> pointGoodsDTOList = pointGoodsService.getPointGoodsDTOList(searchParamet);
        return SmileResult.sucess(pointGoodsDTOList);
    }

    @Override
    @Transactional
    public SmileResult buyGoodsByPoint(String clientNo, Long goodsId) {
        SmileClient client = clientService.getClientInfoByClientNo(clientNo);
        Integer clientPoints = client.getClientPoints();
        SmilePointGoodsDTO pointGoodsDTO = pointGoodsService.getPointGoodsDTO(goodsId);
        if (ObjectUtils.isEmpty(pointGoodsDTO)) {
            return SmileResult.error("商品查询失败");
        }
        Integer goodsPointNum = pointGoodsDTO.getGoodsPointNum();

        if (clientPoints < goodsPointNum) {
            return SmileResult.waring("用户积分不足");
        }
        if (pointGoodsDTO.getGoodsType().equals(GoodsTypeEnum.COUPON.getName())) {
            Long couponRuleId = pointGoodsDTO.getCouponRuleId();
            SmileCoupon coupon = couponService.addCoupon(client.getId(), clientNo, couponRuleId, null);
            expensesPoints.buyPoints(client.getId(),clientNo,goodsPointNum,"优惠券:"+coupon.getCouponNo());
        }else {
            /**
             * 尚未完成目前只有优惠券
             */
        }

        return SmileResult.sucess();
    }

}
